<?php

namespace addons\venue\controller;

use app\admin\model\venue\information\Comment;
use app\admin\model\venue\information\Dz;
use app\admin\model\venue\information\Information;
use app\common\controller\Api;
use think\Request;

class Infor extends Api
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['list','detail'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    protected $config = "";

    public function __construct(Request $request = null)
    {
        $this->config = get_addon_config("venue");
        parent::__construct($request);
    }

    /*
     * 资讯列表
     */
    public function list(){

        $page   = $this->request->post('page')??1;  //页码
        $limit  = $this->request->post('limit')??10; //每页显示条数

        $data = Information::where('state',1)
            ->field("id,title,type,image,startdate,content")
            ->order("weigh","desc")
            ->paginate(["page"=>$page,"list_rows"=>$limit])
            ->each(function ($item){
                $item->image = cdnurl($item->image,true);
                $item->content = strip_tags($item->content);
            });

        $result = array("total" => $data->total(), "rows" => $data->items());
        $this->success('返回成功',$result);
    }

    /*
     * 资讯详情
     */
    public function detail(){
        $id   = $this->request->post('id');
        if (!$id){
            $this->error("缺少资讯id");
        }
        $data = Information::where('state',1)
            ->where('id',$id)
            ->field("id,title,type,image,content,startdate,view_counts,dz_counts")
            ->find();
        $prefix = request()->domain() ;
        // 用正则表达式匹配图片地址并添加前缀
        $updatedHtmlContent = preg_replace('/(<img src=")([^"]*)"/i', '$1' . $prefix . '$2"', $data['content']);
        $data['content'] = $updatedHtmlContent ;
        $data['image'] = cdnurl($data['image'],true) ;
        $data->view_counts += 1;
        $data->save();
        //查询是否已点赞
        $dz = Dz::where('user_id', $this->auth->id)->where('infor_id', $id)->find();
        $data['dz_status'] = $dz? 1 : 0;
        //获取评论列表
        $data['comment'] = Comment::where('infor_id', $id)->where('status',1)->order('id desc')->select()
            ->each(function ($item){
                $item->avatar = cdnurl($item->avatar,true);
                $item->updatetime = venue_format_dates($item->updatetime);
            });
        $this->success('返回成功',$data);
    }

    /*
     * 资讯点赞/取消点赞
     */
    public function dz(){
        $user_id = $this->auth->id;
        $id   = $this->request->post('id');
        if (!$id) {
            $this->error("缺少资讯id");
        }
        $infor = Information::get($id);
        if (!$infor) {
            $this->error("资讯不存在");
        }
        $res = Dz::where('user_id', $user_id)->where('infor_id', $id)->find();
        if ($res) {
            $res->delete();
            $infor->dz_counts -= 1;
            $infor->save();
            $this->success("取消点赞成功");
        } else {
            $dz = new Dz();
            $dz->user_id = $user_id;
            $dz->infor_id = $id;
            $dz->createtime = time();
            $dz->updatetime = time();
            $dz->save();
            $infor->dz_counts += 1;
            $infor->save();
            $this->success("点赞成功");
        }

    }

    /*
     * 评论资讯
     */
    public function comment(){
        $user_id = $this->auth->id;
        $id   = $this->request->post('id');
        $content   = $this->request->post('content');
        if (!$id) {
            $this->error("缺少资讯id");
        }
        if (!$content) {
            $this->error("评论内容不能为空");
        }
        $infor = Information::get($id);
        if (!$infor) {
            $this->error("资讯不存在");
        }
        $comment = new Comment();
        $comment->user_id = $user_id;
        $comment->username = $this->auth->username;
        $comment->infor_id = $id;
        $comment->content = $content;
        $comment->avatar = $this->auth->avatar;
        $comment->createtime = time();
        $comment->updatetime = time();
        $comment->save();
        $infor->save();
        $this->success("评论将在审核通过后显示");
    }




}
